/**
 * Created by 黄忠园 on 2017/12/25.
 */
angular.module('module.repair')
    .controller('RepairController', RepairController);
RepairController.$inject = ['$state', '$localStorage', 'JobJobInfoService', 'WmoperationInfoPromotershopService','NewsService'];
function RepairController($state, $localStorage, JobJobInfoService, WmoperationInfoPromotershopService,NewsService) {

    var vm = this;
    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }
    var shopid = $localStorage[environmental + "shopid"];
    vm.messageVisible = false; // 提示框初始化
    vm.cancelBtn = cancelBtn; // 提示框取消按钮
    vm.sureBtn = sureBtn; // 提示框确认按钮
    vm.sure = sure; // 确认按钮
    vm.num = 0; // 设备数
    vm.text = ""; // 问题描述
    vm.wordsnum = 0; // 问题描述字数
    var devNum = 0;//商户拥有的设备数
    var time = new Date();
    var y = time.getFullYear();
    var m = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
    var d = time.getDate() < 10 ? ('0' + time.getDate()) : time.getDate();
    vm.curtime = y + '-' + m + '-' + d; // 当前时间
    vm.textchange = textchange; // 显示字数
    vm.classlist = ["故障报修", "技术咨询", "其它"]; // 问题分类数组
    vm.newchose = newchose; // 选取分类事件
    var doc = document.getElementsByName("classdetail"); // 获取分类数组
    vm.storage = ""; // 存储分类事件结果
    vm.prolist = ["按键", "显示屏", "后盖", "电池", "其它"]; // 快捷选择数组
    vm.chose = chose; // 选择损坏部位事件
    var list = []; // 存贮损坏部位事件结果
    vm.focus = focus; // 聚焦更改设备数

    // 获取teamid
    WmoperationInfoPromotershopService.getTeamId(shopid).$promise.then(function (data) {
        vm.teamid = data[0].directid;
    });
    //获取商户拥有设备数
    NewsService.getShopinfoByShopid(shopid).$promise.then(function (data) {
        devNum = data.devidNumber;
    },function (error) {
        vm.messageVisible = true;
        vm.message = "获取商户设备数失败！"
    })


    // 显示字数
    function textchange() {
        vm.wordsnum = vm.text.length;
    }

    // 选取分类事件
    function newchose(index) {
        for (var i = 0; i < vm.classlist.length; i++) {
            if (i == index) {
                doc[i].setAttribute("style", cssactive);
                vm.storage = vm.classlist[index];
            } else {
                doc[i].setAttribute("style", cssdefault);
            }
        }
    }

    // 选择损坏部位
    function chose(event, pro) {
        list = storageChose(event, pro, list);
    }

    function focus() {
        vm.num = null;
    }

    // 确认按钮
    function sure(num, text) {
        if (!vm.storage) {
            vm.messageVisible = true;
            vm.message = "请选择问题类型";
        }
        else if ((!num || num == 0) && vm.storage == "故障报修") {
            vm.messageVisible = true;
            vm.message = "请选择损坏设备数";
        }
        else if ((num > devNum) && vm.storage == "故障报修") {
            vm.messageVisible = true;
            vm.message = "设备数超过商户拥有设备数";
        }
        else if (text.length == 0) {
            vm.messageVisible = true;
            vm.message = "请输入问题描述";
        }
        else if (0 < text.length && text.length < 6 && vm.storage == "其它") {
            vm.messageVisible = true;
            vm.message = "请输入问题描述,最少6个字";
        }
        else if (text.length > 100) {
            vm.messageVisible = true;
            vm.message = "问题描述字数大于100";
        }
        else {
            if (vm.storage != "故障报修") {
                list = [];
                vm.num = 0;
            }else if(list.length == 0){
                vm.messageVisible = true;
                vm.message = "请选择损坏部位";
            }else {
                var common = {
                    sort: list, // 快捷标签列表
                    badnum: vm.num // 损坏台数
                };
                var info = {
                    "shopid": shopid,
                    "platformid": platformid,
                    "teamid": vm.teamid,
                    "dutyid": 0,
                    "jobstatus": 0, // (0：未受理，1：处理中，2已处理，3已确认，4已评价，5取消)
                    "title": vm.storage, // 问题标题
                    "detail": vm.text, // 问题描述
                    "commonality": JSON.stringify(common)
                };
                JobJobInfoService.addJob(info).$promise.then(function (data) {
                    $state.go("repairrecord");
                }, function (errorinfo) {
                    vm.messageVisible = true;
                    vm.message = JSON.stringify(errorinfo);
                })
            }
        }
    }

    // 提示框取消按钮函数
    function cancelBtn() {
        vm.messageVisible = false;
    }

    // 提示框确认按钮函数
    function sureBtn() {
        vm.messageVisible = false;
    }
}